package com.lbn.monitor.app.history;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lbn.monitor.R;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.app.history.adapter.HistoryAdapter;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.base.AppBaseFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrFrameLayout;
import indi.free.widgets.custom_recycler_view.BasePagerRecyclerAdapter;

public class TableFragment extends AppBaseFragment<TablePresenter> implements ITableView<List<RealTimeValue>>, AppBaseActivity.OnPtrRefresh,
        BasePagerRecyclerAdapter.OnLoadMoreListener {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @BindView(R.id.cod_standard)
    TextView codStandard;
    @BindView(R.id.unit_standard)
    TextView unitStandard;
    @BindView(R.id.cod_observe)
    TextView codObserve;
    @BindView(R.id.unit_observe)
    TextView unitObserve;

    private HistoryAdapter adapter;
    private HistoryActivity act;

    public HistoryAdapter getAdapter() {
        return adapter;
    }

    public PtrFrameLayout getPtrFrame() {
        return ptrFrame;
    }

    public static TableFragment newInstance() {
        Bundle args = new Bundle();
        TableFragment fragment = new TableFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_table, null, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        act = (HistoryActivity) getActivity();
        adapter = new HistoryAdapter(getContext(), recyclerView, null);
        adapter.setmOnLoadMoreListener(this);
//        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        initPtr(ptrFrame, this, false);

    }

    @Override
    protected TablePresenter createPresenter() {
        return new TablePresenter();
    }

    @Override
    public void refreshComplete(List<RealTimeValue> data) {
        adapter.refreshData(data, !(data == null || data.size() < 20));
        ptrFrame.refreshComplete();
        setCod(act.getWuran());
        setUnit(act.getUnit());
    }

    @Override
    public void addMoreData(List<RealTimeValue> data, boolean isMore) {
        adapter.addMoreData(data, !(data == null || data.size() < 20));
    }

    @Override
    public void onPtrRefresh() {
        act.onRefresh();
    }

    @Override
    public void onLoadMore() {
        act.onLoadMore();
    }


    public void setCod(String cod) {
        codStandard.setText(cod);
        codObserve.setText(cod);
    }

    public void setUnit(String unit) {
        unitStandard.setText(unit);
        unitObserve.setText(unit);
    }
}
